package com.mineapp.model

import android.os.Parcel
import android.os.Parcelable

/**
 * 作者： Ryan
 * 创建时间： 15/9/7 10:24
 * 类描述：商品
 */
class Goods() : Parcelable {

    /**商品创建时间 */
    var crtime: Long = 0
    /** 商品 id */
    var dmId: Long = 0
    /**商品都在分类 id */
    var goodsCategoryId: Long = 0
    /**商品名称 */
    var goodsName: String? = null
    /**产品照片 */
    var imgs: String? = null
    /**每日限量 */
    var limited: Int = 0
    /**销售数量 */
    var sales: Int = 0
    /**商品所在的商店 id */
    var sellerStoreId: Long = 0
    /**(0：上架；1：下架；3：强制下架 */
    var status: Int = 0
    /**商品主人 id */
    var userId: Long = 0
    /**产品价格 */
    var price: Double = 0.toDouble()
    /**选中数量 */
    var selectCount: Int = 0

    var isCoupon: Int = 0

    private var mAction: Int = 0
    /**商品运费 */
    var freight: Int = 0

    var goodsDetail: String? = null
    /**产品属性列表 */
    var properties: List<GoodsNorms>? = null
    var attrMap: Map<Long, Long>? = null

    //商品主图连接
    var mainPicture: String? = null
    //是否推荐商品 0.是 1.否
    var isRecommend: Int = 0
    //一级分类dmId
    var oneCategoryId: Long = 0
    //二级分类
    var twoCategoryId: Long = 0

    constructor(parcel: Parcel) : this() {
        crtime = parcel.readLong()
        dmId = parcel.readLong()
        goodsCategoryId = parcel.readLong()
        goodsName = parcel.readString()
        imgs = parcel.readString()
        limited = parcel.readInt()
        sales = parcel.readInt()
        sellerStoreId = parcel.readLong()
        status = parcel.readInt()
        userId = parcel.readLong()
        price = parcel.readDouble()
        selectCount = parcel.readInt()
        isCoupon = parcel.readInt()
        mAction = parcel.readInt()
        freight = parcel.readInt()
        goodsDetail = parcel.readString()
        mainPicture = parcel.readString()
        isRecommend = parcel.readInt()
        oneCategoryId = parcel.readLong()
        twoCategoryId = parcel.readLong()
    }

    fun getmAction(): Int {
        return mAction
    }

    fun setmAction(mAction: Int) {
        this.mAction = mAction
    }

    override fun equals(o: Any?): Boolean {
        if (o != null && o is Goods) {
            val another = o as Goods?
            if (another!!.properties != null && another.properties!!.size > 0) {
                return another.dmId == dmId && isPropertySame(another.attrMap,
                        this.attrMap)
            } else {
                return another.dmId == dmId
            }
        }

        return super.equals(o)
    }

    private fun isPropertySame(a1: Map<Long, Long>?, a2: Map<Long, Long>?): Boolean {
        if (null == a1 || null == a2 || a1.isEmpty() || a2.isEmpty()
                || a1.size != a2.size)
            return false
        for ((key, value) in a1) {
            if (!a2.containsKey(key) || !a2.containsValue(value)) {
                return false
            }
        }
        return true
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeLong(crtime)
        parcel.writeLong(dmId)
        parcel.writeLong(goodsCategoryId)
        parcel.writeString(goodsName)
        parcel.writeString(imgs)
        parcel.writeInt(limited)
        parcel.writeInt(sales)
        parcel.writeLong(sellerStoreId)
        parcel.writeInt(status)
        parcel.writeLong(userId)
        parcel.writeDouble(price)
        parcel.writeInt(selectCount)
        parcel.writeInt(isCoupon)
        parcel.writeInt(mAction)
        parcel.writeInt(freight)
        parcel.writeString(goodsDetail)
        parcel.writeString(mainPicture)
        parcel.writeInt(isRecommend)
        parcel.writeLong(oneCategoryId)
        parcel.writeLong(twoCategoryId)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<Goods> {
        override fun createFromParcel(parcel: Parcel): Goods {
            return Goods(parcel)
        }

        override fun newArray(size: Int): Array<Goods?> {
            return arrayOfNulls(size)
        }

        class GoodsNorms {
            var property: String? = null //标题
            var childProperties: List<GoodsInfo>? = null //规格组
            var dmId: Long = 0 //规格id

            override fun equals(obj: Any?): Boolean {
                if (obj == null) return false
                if (obj is GoodsNorms) {
                    val other = obj as GoodsNorms?
                    val infoList = other!!.childProperties
                    if (other.dmId != dmId || infoList == null || infoList.size != childProperties!!.size)
                        return false
                    for (goodsInfo in infoList) {
                        if (!childProperties!!.contains(goodsInfo)) {
                            return false
                        }
                    }

                } else {
                    return false
                }
                return true
            }
            companion object {
                class GoodsInfo {
                    var property: String? = null //名称
                    var dmId: Long = 0     //id
                    var isSelect = false //是否选中

                    override fun equals(obj: Any?): Boolean {
                        if (obj is GoodsInfo) {
                            val other = obj as GoodsInfo?
                            if (other!!.dmId == dmId) return true
                        }
                        return false
                    }

                }
            }
        }
    }

}
